package com.bfly.mall.product.dto;

import com.bfly.core.config.ResourceConfigure;
import org.apache.commons.lang3.StringUtils;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Comparator;
import java.util.List;

/**
 * 产品表单DTO
 *
 * @author 长沙商脉科技有限公司
 * @date 2020/9/15 21:23
 */
public class ProductFormDTO implements Serializable {

    private static final long serialVersionUID = 4278237350977504898L;

    private int id;
    @NotBlank(message = "产品名称不能为空!")
    private String name;

    @NotBlank(message = "产品封面不能为空!")
    private String cover;

    private int categoryId;
    private int freightId;

    @NotBlank(message = "产品单位不能为空!")
    private String unit;
    private int tag;
    private boolean onShelf;
    private int virtualSales;
    private int seq;
    private ProductTxtFormDTO txt;
    private List<ProductImgFormDTO> imgs;
    private List<ProductAttrFormDTO> attrs;

    @NotEmpty(message = "产品规格不能为空!")
    private List<ProductSpeAttrFormDTO> speAttrs;

    @NotEmpty(message = "产品SKU不能为空!")
    private List<ProductSkuFormDTO> skus;

    public String getUrl() {
        return StringUtils.isNotBlank(getCover()) ? ResourceConfigure.getResourceHttpUrl(getCover()) : "";
    }

    /**
     * 获得最低价格规格的产品
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/9/23 15:20
     */
    private ProductSkuFormDTO getMinPriceSku() {
        List<ProductSkuFormDTO> skus = getSkus();
        skus.sort(Comparator.comparing(ProductSkuFormDTO::getPrice));
        return skus.get(0);
    }

    /**
     * 显示最低价钱规格的产品原始价格信息
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/9/23 14:54
     */
    public BigDecimal getOriginalPrice() {
        return getMinPriceSku().getOriginalPrice();
    }

    /**
     * 显示最低价钱规格的产品价格信息
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/9/23 14:58
     */
    public BigDecimal getPrice() {
        return getMinPriceSku().getPrice();
    }

    /**
     * 显示最低加钱规格的产品VIP价格信息
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/9/23 14:58
     */
    public BigDecimal getVipPrice() {
        return getMinPriceSku().getVipPrice();
    }

    /**
     * 显示总库存
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/9/23 14:59
     */
    public int getTotalStock() {
        int total = getSkus().stream().mapToInt(ProductSkuFormDTO::getStock).sum();
        return total;
    }

    public int getSeq() {
        return seq;
    }

    public void setSeq(int seq) {
        this.seq = seq;
    }

    public int getTag() {
        return tag;
    }

    public void setTag(int tag) {
        this.tag = tag;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCover() {
        return cover;
    }

    public void setCover(String cover) {
        this.cover = cover;
    }

    public int getCategoryId() {
        return categoryId;
    }

    public void setCategoryId(int categoryId) {
        this.categoryId = categoryId;
    }

    public int getFreightId() {
        return freightId;
    }

    public void setFreightId(int freightId) {
        this.freightId = freightId;
    }

    public String getUnit() {
        return unit;
    }

    public void setUnit(String unit) {
        this.unit = unit;
    }

    public boolean isOnShelf() {
        return onShelf;
    }

    public void setOnShelf(boolean onShelf) {
        this.onShelf = onShelf;
    }

    public int getVirtualSales() {
        return virtualSales;
    }

    public void setVirtualSales(int virtualSales) {
        this.virtualSales = virtualSales;
    }

    public ProductTxtFormDTO getTxt() {
        return txt;
    }

    public void setTxt(ProductTxtFormDTO txt) {
        this.txt = txt;
    }

    public List<ProductImgFormDTO> getImgs() {
        return imgs;
    }

    public void setImgs(List<ProductImgFormDTO> imgs) {
        this.imgs = imgs;
    }

    public List<ProductAttrFormDTO> getAttrs() {
        return attrs;
    }

    public void setAttrs(List<ProductAttrFormDTO> attrs) {
        this.attrs = attrs;
    }

    public List<ProductSpeAttrFormDTO> getSpeAttrs() {
        return speAttrs;
    }

    public void setSpeAttrs(List<ProductSpeAttrFormDTO> speAttrs) {
        this.speAttrs = speAttrs;
    }

    public List<ProductSkuFormDTO> getSkus() {
        return skus;
    }

    public void setSkus(List<ProductSkuFormDTO> skus) {
        this.skus = skus;
    }
}
